package com.simpleframe.persistence.hibernate;

import java.util.List;

/**
 * 分页类，参考自JavaEye及SpringSide
 */
@SuppressWarnings("unchecked")
public class Page {
    public final static int PAGESIZE = 10;
    
    //每页记录数
    private int pageSize = PAGESIZE;
    
    private String pageOrder;

    private List items;

    //记录总数
    private int totalCount;

    private int[] indexes = new int[0];
    
    //
    private int startIndex = 0;

    public Page(List items, int totalCount) {
        setPageSize(PAGESIZE);
        setTotalCount(totalCount);
        setItems(items);
        setStartIndex(0);
    }

//    public Page(List items, int totalCount, int startIndex) {
//        setPageSize(PAGESIZE);
//        setTotalCount(totalCount);
//        setItems(items);
//        setStartIndex(startIndex);
//    }

    public Page(List items, String pageOrder ,int totalCount, int pageSize, int startIndex) {
    	this.pageOrder = pageOrder;
        setPageSize(pageSize);
        setTotalCount(totalCount);
        setItems(items);
        setStartIndex(startIndex);
    }

    /**
     * 将页码转换为列表的startIndex，页大小为默认大小
     */
    public static int convertFromPageToStartIndex(int pageNo) {
        return (pageNo - 1) * PAGESIZE;
    }
    
    /**
     * 将页码转换为列表的startIndex
     */
    public static int convertFromPageToStartIndex(int pageNo, int pageSize) {
        return (pageNo - 1) * pageSize;
    }

    public List getItems() {
        return items;
    }

    public void setItems(List items) {
        this.items = items;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    /**
     * 设置数据总数，并计算各页起始位置
     */
    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            int count = totalCount / pageSize;
            if (totalCount % pageSize > 0)
                count++;
            indexes = new int[count];
            for (int i = 0; i < count; i++) {
                indexes[i] = pageSize * i;
            }
        } else {
            this.totalCount = 0;
        }
    }

    public int[] getIndexes() {
        return indexes;
    }

    public void setIndexes(int[] indexes) {
        this.indexes = indexes;
    }

    public int getStartIndex() {
        return startIndex;
    }

    /**
     * 设置当前起始位置
     */
    public void setStartIndex(int startIndex) {
        if (totalCount <= 0)
            this.startIndex = 0;
        else if (startIndex >= totalCount)
            this.startIndex = indexes[indexes.length - 1];
        else if (startIndex < 0)
            this.startIndex = 0;
        else {
            this.startIndex = indexes[startIndex / pageSize];
        }
    }

    /**
     * 获得下页起始位置
     */
    public int getNextIndex() {
        int nextIndex = getStartIndex() + pageSize;
        if (nextIndex >= totalCount)
            return getStartIndex();
        else
            return nextIndex;
    }

    /**
     * 获得上页起始位置
     */
    public int getPreviousIndex() {
        int previousIndex = getStartIndex() - pageSize;
        if (previousIndex < 0)
            return 0;
        else
            return previousIndex;
    }

    /**
     * 取总页数.
     */
    public long getTotalPageCount() {
        if (totalCount % pageSize == 0)
            return totalCount / pageSize;
        else
            return totalCount / pageSize + 1;
    }

    /**
     * 取该页当前页码,页码从1开始.
     */
    public long getCurrentPageNo() {
        return startIndex / pageSize + 1;
    }

    /**
     * 该页是否有下一页.
     */
    public boolean hasNextPage() {
        return this.getCurrentPageNo() < this.getTotalPageCount() ;//- 1;
    }

    /**
     * 该页是否有上一页.
     */
    public boolean hasPreviousPage() {
        return this.getCurrentPageNo() > 1;
    }
    
    public String getPageOrder(){
    	if(pageOrder!=null){
    		return pageOrder;
    	}
    	return "";
    }
    
    ///////////////////////
    
    /**
	 * 后台分页
	 * @param Count
	 * @param curPage
	 * @param perPage
	 * @param url
	 * @return
	 */
	public String getToolBar(){
		
		StringBuffer html = new StringBuffer();
		//判断项目是否有对应的模块
		
		
		html.append("显示 ");
		html.append("<select onchange='javascript:$(\\\"#pageSize\\\").val($(this).val());jumpPage(1)'>"+getPageSizeOption(pageSize)+"<select>");
		html.append(" 条， ");
		html.append("共 " + getTotalPageCount() + " 页 ");
		html.append("第 " + getCurrentPageNo()+" 页 ");
		
		html.append("<span class='tool_divider'></span>");
		
		if (hasPreviousPage()){
			html.append(getHref(1, "首页"));
			html.append(getHref(getCurrentPageNo()-1, "上一页"));
		}else{
			html.append("首页 上一页 ");
		}
		if (hasNextPage()){
			html.append(getHref(getCurrentPageNo()+1, "下一页"));
			html.append(getHref(getTotalPageCount(), "尾页"));
		}else{
			html.append("下一页 尾页 ");
		}
		
		
		html.append("<span class='tool_divider'></span>");
		
		html.append("第 ");
		html.append("<input type='text' id='pageNoValue' size='1' />");
		html.append(" 页&nbsp;");
		html.append("<a href='javascript:jumpPage(($(\\\"#pageNoValue\\\").val()))' class='on'>&nbsp;跳转&nbsp;</a>");
		
		StringBuffer finals = new StringBuffer();
		finals.append("<input type='hidden' name='pageNo' id='pageNo' value='"+getCurrentPageNo()+"'/>");
		finals.append("<input type='hidden' name='pageSize' id='pageSize' value='"+pageSize+"'/>");
		finals.append("<input type='hidden' name='pageOrder' id='pageOrder' value='"+getPageOrder()+"' />");
		finals.append("<script>$('#page').html(\"");
		finals.append(html);
		finals.append("\")</script>");
		
		return finals.toString();
	}
	
	private String getPageSizeOption(int size){
		StringBuffer options = new StringBuffer();
		int[] sizes = {10,30,50,70};
		for (int i : sizes) {
			if(size==i)
				options.append("<option selected>"+i+"</option>");
			else {
				options.append("<option>"+i+"</option>");
			}
		}
		return options.toString();
	}
	
	private String getHref(Long pageNo,String title){
		return "<a href='javascript:jumpPage("+pageNo+");'>"+title+"</a>";
	}
	
	private String getHref(int pageNo, String title){
		return "<a href='javascript:jumpPage("+pageNo+");'>"+title+"</a>";
	}
	
}
